﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Tunnel_Cross_Section.相关类;
using Tunnel_Cross_Section.option_frm;
using Tunnel_Cross_Section.menue_frm;
using DevComponents.AdvTree;
using DevComponents.DotNetBar;
using TunnelClasses;
using System.Threading;
using ZedGraph;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using Excel;


namespace Tunnel_Cross_Section.menue_frm
{
    public partial class do_section_frm : Office2007Form
    {
        public Mfrm mfrm;
        public DataBase m_db;
        public int no;
        public Projects m_prj;
        public CAD_Class cad;
        public string LR = "";
        bool working = false;
        #region 构造函数
        void start_cad()
        {
            if (cad == null)
            {


                if (mfrm.cad == null)
                {
                    

                    mfrm.progressBar.Maximum = 3;
                    mfrm.progressBar.PerformStep();
                    mfrm.progressBar.Visible = true;
                    mfrm.Label.Text = "正在启动CAD";
                    cad = new CAD_Class();
                    mfrm.progressBar.PerformStep();
                    Thread.Sleep(400);
                    mfrm.progressBar.PerformStep();
                    mfrm.progressBar.Visible = false;
                    mfrm.progressBar.Value = 0;
                    mfrm.Label.Text = "开始进行收敛处理";
                    mfrm.cad = cad;
                }
                else
                {
                    cad = mfrm.cad;
                }
            }
            left_btn_read.Enabled = true;
            right_btn_read.Enabled = true;
            btn_add_in.Enabled = true;

        }
        public do_section_frm(DataBase db,Mfrm frm)
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
            m_db = db;
            mfrm = frm;
          
            select_period_frm p_frm = new select_period_frm(mfrm, this, m_db);
            p_frm.ShowDialog();
            if (no == 0)
            {
                this.Close();
                return;
            }
            
            Thread thr = new Thread(new ThreadStart(start_cad));
            thr.Start();
            this.Text = mfrm.cur_prj.section_name + "  第" + no.ToString() + "期";
            m_prj = mfrm.cur_prj;
            m_prj.no = no;
            creat_table();
            //关于zedgraph的一些设置
            zed.GraphPane.Chart.Fill = new Fill(Color.White, Color.FromArgb(238, 243, 250), 45.0F);//255, 255, 166
            GraphPane myPane = zed.GraphPane;
            myPane.XAxis.MajorGrid.IsVisible = true;
            myPane.YAxis.MajorGrid.IsVisible = true;
            myPane.XAxis.Title.Text = "H坐标";
            myPane.YAxis.Title.Text = "V坐标";
            zedac.GraphPane.Chart.Fill = new Fill(Color.White, Color.FromArgb(238, 243, 250), 45.0F);//255, 120, 166
            zed.GraphPane.YAxis.Scale.Max = 3;
            zed.GraphPane.YAxis.Scale.Min = -3;
            zed.GraphPane.XAxis.Scale.Max = 3 * zed.GraphPane.Chart.Rect.Width / zed.GraphPane.Chart.Rect.Height;
            zed.GraphPane.XAxis.Scale.Min = -3 * zed.GraphPane.Chart.Rect.Width / zed.GraphPane.Chart.Rect.Height;
            zed.GraphPane.XAxis.Scale.MajorStep = 0.5;
            zed.GraphPane.XAxis.Scale.MinorStep = 0.25;
            zed.GraphPane.YAxis.Scale.MajorStep = 0.5;
            zed.GraphPane.YAxis.Scale.MinorStep = 0.25;
            zed.GraphPane.IsPenWidthScaled = false;
            zed.Refresh();

            myPane = zedac.GraphPane;
           // myPane.XAxis.MajorGrid.IsVisible = true;
            myPane.YAxis.MajorGrid.IsVisible = true;
            myPane.XAxis.Title.Text = "角度";
            myPane.YAxis.Title.Text = "变形(mm)";
            myPane.Legend.IsVisible = false;
            zeddesign.GraphPane.Chart.Fill = new Fill(Color.White, Color.FromArgb(238, 243, 250), 45.0F);

            myPane = zeddesign.GraphPane;
           // myPane.XAxis.MajorGrid.IsVisible = true;
            myPane.YAxis.MajorGrid.IsVisible = true;
            myPane.XAxis.Title.Text = "角度";
            myPane.YAxis.Title.Text = "变形(mm)";
            myPane.Legend.IsVisible = false;
            //接着LoadTree
            Load_tree();
            Load_unsolved();


        }
        #endregion

        #region 读取配置的excel文件,文件内容包括里程,沉降点号,环号
        private void btn_config_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Filter = "Excel 文件 (*.xls)|*xls|所有文件 (*.*)|*.*";
            if (op.ShowDialog() == DialogResult.OK)
            {
              
                string filename = op.FileName;
                ApplicationClass excel = new ApplicationClass();
                mfrm.Label.Text = "开始读取里程信息,请稍等...";
                this.UseWaitCursor = true;
                Worksheet Lsheet;
                Worksheet Rsheet;
                Workbook m_Book;
                object miss = Type.Missing;
                m_Book = excel.Workbooks.Open(filename, miss, miss, miss, miss, miss,
                    miss, miss, miss, miss, miss, miss, miss, miss, miss);
                Lsheet = m_Book.Worksheets[1] as Worksheet;
                Rsheet = m_Book.Worksheets[2] as Worksheet;
                Load_Lc(Lsheet);
                Load_Lc(Rsheet);
                excel.Quit();
                mfrm.Label.Text = "里程信息读取完毕!";
                this.UseWaitCursor = false;
            }
        }
        private void Load_Lc(Worksheet sh)
        {
            Range r = sh.Cells[2, 1] as Range;

            string id = r.Value2.ToString();
            if (id.Contains("L") == true || id.Contains("l") == true)
            {
                LR = "L";
            }
            else
            {
                LR = "R";
            }
            while (r.Value2 != null && r.Value2.ToString().Length > 0)
            {
                id = r.Value2.ToString();
                double lc = Convert.ToDouble(r.get_Offset(0, 3).Value2);
                int hh = Convert.ToInt32(r.get_Offset(0, 1).Value2);
                string cj = r.get_Offset(0, 2).Value2.ToString();
                string table = "qj_" + m_prj.id;
                string sql = "Select [里程] from " + table + " Where [断面号]='" + id + "'";
                System.Data.DataTable dt = m_db.GetDataSet(sql).Tables[0];
                if (dt == null || dt.Rows.Count == 0)//当其中没有数据
                {

                    sql = "Insert into " + table + " values('" + id + "'," + hh + "," + lc + ",'" + cj + "','" + LR + "'," + -1 + "," + -1
                        + "," + -1 + "," + -1 + ",'','')";
                    m_db.ExecuteSQL(sql);
                }
                else//当其中已有数据
                {
                    sql = "Update [" + table + "] Set [环号]=" + hh + ",[里程]=" + lc + ",[沉降点号]='" + cj
                                           + "' Where [断面号]='" + id + "'";
                    m_db.ExecuteSQL(sql);
                }
                r = r.get_Offset(1, 0);
            }
        }
        #endregion

        #region 找找表,如果不存在就要新建一个
        private void creat_table()
        {
            string table_name = "qj_" + m_prj.id.ToString();
            if (m_db.IsTableInDataBase(table_name) == false)
            {
                string sql = "Select * into [" + table_name + "] from [id]";
                m_db.ExecuteSQL(sql);
            }
            table_name = "qj_" + m_prj.id.ToString() + "_" + m_prj.no.ToString();
            if (m_db.IsTableInDataBase(table_name) == false)
            {
                string sql = "Select * into [" + table_name + "] from [id_no]";
                m_db.ExecuteSQL(sql);
            }
            table_name = "qj_" + m_prj.id.ToString() + "_" + m_prj.no.ToString() + "_data";
            if (m_db.IsTableInDataBase(table_name) == false)
            {
                string sql = "Select * into [" + table_name + "] from [id_no_data]";
                m_db.ExecuteSQL(sql);
            }
        }
        #endregion


        #region 核心功能:断面处理

        //读入上行线数据,恩
        private void left_btn_read_Click(object sender, EventArgs e)
        {
            if (working == true)
            {
                message_frm frm = new message_frm("程序正在处理断面数据,\r\n请不要瞎点!", "警告", 2);
                frm.ShowDialog();
                frm.Close();
                return;
            }

            if (unsolve.l_List != null && unsolve.l_List.Count > 0)
            {
                message_frm frm = new message_frm("尚有未处理成功的断面需要操作,\r\n确认继续操作吗?", "提示", 1);
                frm.ShowDialog();
                if (frm.yesorno == false)
                {
                    frm.Close();
                    return;
                }
                frm.Close();
                unsolve.l_List.Clear();
                unsolve_tree.Nodes.Clear();
               
            }
            LR = "L";
         
            Thread th = new Thread(new ThreadStart(Process_Sections));
            th.Start();

        }
        //恩恩,下行线数据
        private void right_btn_read_Click(object sender, EventArgs e)
        {
            if (working == true)
            {
                message_frm frm = new message_frm("程序正在处理断面数据,\r\n请不要瞎点!", "警告", 2);
                frm.ShowDialog();
                frm.Close();
                return;
            }

            if (unsolve.r_List != null && unsolve.r_List.Count > 0)
            {
                message_frm frm = new message_frm("尚有未处理成功的断面需要操作,\r\n确认继续操作吗?", "提示", 1);
                frm.ShowDialog();
                if (frm.yesorno == false)
                {
                    frm.Close();
                    return;
                }
                frm.Close();
                unsolve.r_List.Clear();
                unsolve_tree.Nodes.Clear();
            }
            LR = "R";
            
            Thread th = new Thread(new ThreadStart(Process_Sections));
            th.Start();
        }
        //处理好几个断面
        void Process_Sections()
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Filter = "RAW 文件 (*.raw)|*raw|所有文件 (*.*)|*.*";
            op.Multiselect = true;
            if (op.ShowDialog() == DialogResult.OK)
            {
                working = true;
                advTree1.Nodes.Clear();
                mfrm.UseWaitCursor = true;
                
               
                int max=op.FileNames.Length;
                mfrm.progressBar.Maximum =max ;
                mfrm.progressBar.Visible = true;
                for (int i = 0; i < max; i++)
                {
                    mfrm.progressBar.PerformStep();
                    mfrm.Label.Text = (i + 1).ToString() + "/" + max.ToString();
                    Process_Section(op.FileNames[i]);
                  
                }
              
                Load_tree();
                mfrm.UseWaitCursor = false;
                mfrm.progressBar.Visible = false;
                mfrm.progressBar.Value = 0;//progressbar清零
                working = false;
                if (LR == "L")
                {
                    mfrm.Label.Text = "收敛操作已完成! 成功:" + (max - unsolve.l_List.Count).ToString() + "个;失败"
                        + unsolve.l_List.Count.ToString() + "个";
                }
                else
                {
                    mfrm.Label.Text = "收敛操作已完成! 成功:" + (max - unsolve.r_List.Count).ToString() + "个;失败"
                                          + unsolve.r_List.Count.ToString() + "个";
                }
                mfrm.Label.Refresh();     
                Load_unsolved();
              //  tabControl.SelectedTabIndex = 1;

            }
        }

        //处理一个断面
        public unsolve_list unsolve = new unsolve_list();//声明个变量,储存顶狐没数据的对象.
       public void Process_Section(object filename1)
        {           
            
            string filename = filename1.ToString();
            double dh = 0;
           // bool exsit=false;
            string table_name = "qj_" + m_prj.id.ToString();
            string sql;
            tunnelSection section = new tunnelSection(filename);
            section.SectNo = LR + section.SectNo;
            if (m_prj.no == 1)
            {
                sql = "Delete from [" + table_name + "]  where [断面号]='" + section.SectNo + "'";
                m_db.ExecuteSQL(sql);
            }
            sql = "select [BH],[BV],[H],[V] from [" + table_name + "] where [断面号]='" + section.SectNo + "'";
            System.Data.DataTable dt = m_db.GetDataSet(sql).Tables[0];
            double H, V, BH, BV;
            string sql_HVBHBV="";
            if (dt.Rows.Count > 0 && dt.Rows[0][0].ToString() != "-1" && dt.Rows[0][0].ToString().Length>0)
            {
                
                //如果坐标系已经存在,那么就平移坐标,并且赋值.
               // exsit = true;
                BH = Convert.ToDouble(dt.Rows[0][0]);
                BV = Convert.ToDouble(dt.Rows[0][1]);
                H = Convert.ToDouble(dt.Rows[0][2]);
                V = Convert.ToDouble(dt.Rows[0][3]);
                section.ButtomCenter = new TSPCoor(V, H);
                double dv = BV + section.SectPnts.InstrHeight;
                dh = BH;
                section.SectPnts.Adjudt_Unified(dv);


            }
            else
            {
                BH = 0;
                BV = 0 - section.SectPnts.InstrHeight;
                H = 0;
                V = 0;
                
                //如果屁都没有
                if (dt.Rows.Count == 0)
                {
                    sql_HVBHBV = "Insert into [" + table_name + "] values('" + section.SectNo + "',-1,-1,'','" + LR + "'," + H + "," + V
                        + "," + BH + "," + BV + ",'','')";
                  
                }
                //如果有数据,但是没有坐标系的参数
                else if (dt.Rows[0]["BH"].ToString() == "-1" || dt.Rows[0]["BH"].ToString().Length == 0)
                {
                    sql_HVBHBV = "Update [" + table_name + "] Set [BV]=" + BV + ",[H]=" + H + ",[V]=" + V
                        + " Where [断面号]='" + section.SectNo + "'";
                   
                }
          
            }
             
            section.SectPnts.SortByHv();
           // section.Kill_Main_Err();
            List<TunnelSectionPoints> Section_Pts;
            Section_Pts = section.eleminate_Err();

            //对剔除粗差的结果进行一个检验判断
            //如果顶狐没数据,则退出该函数
            for (int i = 0; i < Section_Pts.Count; i++)
            {
                if (Section_Pts[i].Points.Count == 0)
                {
                    unsolved un = new unsolved(filename);
                    un.name = section.SectNo;
                    if (i == 2)
                    {
                        un.reason = "弧顶观测点数量不足";
                    }
                    else
                    {
                        un.reason = "第" + (i + 1).ToString() + "弧计算出现错误";
                    }
                    if (LR == "L")
                    {
                        unsolve.l_List.Add(un);
                    }
                    else
                    {
                        unsolve.r_List.Add(un);
                    }
                    return;

                }
                if (Math.Abs(Section_Pts[i].Radius - 2.75) > 0.3)
                {
                    unsolved un = new unsolved(filename);
                    un.name = section.SectNo;
                    un.reason = "第" + (i + 1).ToString() + "弧拟合偏差太大,可能有误!";
                    if (LR == "L")
                    {
                        unsolve.l_List.Add(un);
                    }
                    else
                    {
                        unsolve.r_List.Add(un);
                    }
                    return;

                }
            }
           //如果程序认为计算拟合的参数和坐标系没有错误,才将这些数据存入数据库
            if (sql_HVBHBV.Length > 0)
            {
                m_db.ExecuteSQL(sql_HVBHBV);
            }


            //计算基础数据,入库,包括每段弧的起始和结束角度,以及每段弧的拟合半径
            table_name=table_name+ "_" + no.ToString();
            sql = "Delete from [" + table_name + "] where [断面号]='" + section.SectNo + "'";
            m_db.ExecuteSQL(sql);
            sql = "Insert into [" + table_name + "] values('" + section.SectNo + "'";
            double[] angles = new double[4];
            for (int i = 0; i < Section_Pts.Count; i++)
            {
                sql = sql + "," + Section_Pts[i].center.Coor_H + "," + Section_Pts[i].center.Coor_V + "," + Section_Pts[i].Radius;

                if (i > 0)
                {
                    angles[i-1] = (Section_Pts[i - 1].end_hv + Section_Pts[i].start_hv) / 2;
                }
            }
            for (int i = 0; i < angles.Length; i++)
            {
                sql = sql + "," + angles[i];
            }
            sql += ")";
            m_db.ExecuteSQL(sql);

            //计算变形,入库
           //(Section_Pts, section.ButtomCenter);
            cad.Section_Pts = Section_Pts;
           //这里用的是四个圆的中心,看看效果如何
            if (no == 1 || H == 0)
            {
                cad.center = section.Four_circle_center;
                H = cad.center.Coor_H;
                V = cad.center.Coor_V;
                string t_name = "qj_" + m_prj.id.ToString();
                sql_HVBHBV = "Update [" + t_name + "] Set [H]=" + H + ",[V]=" + V
        + " Where [断面号]='" + section.SectNo + "'";
                m_db.ExecuteSQL(sql_HVBHBV);
            }
            else
            {
                if (Math.Abs(V - section.Four_circle_center.Coor_V) < 0.01)
                {
                    cad.center = new TSPCoor(V, H);
                }
                else
                {
                    unsolved un = new unsolved(filename);
                    un.name = section.SectNo;
                    un.reason = "仪器高不准确,已用本次拟合圆心代替";
                    if (LR == "L")
                    {
                        unsolve.l_List.Add(un);
                    }
                    else
                    {
                        unsolve.r_List.Add(un);
                    }
                    cad.center = section.Four_circle_center;
                }

                //存到数据库里,下次就按照这个点来进行比较.

            }
            cad.config(section);
            TunnelSectionPoints pts = cad.OutPut_Result();
            table_name += "_data";
            sql = "Delete from [" + table_name + "] where [断面号]='" + section.SectNo + "'";
            m_db.ExecuteSQL(sql);
            for (int i = 0; i < pts.Points.Count; i++)
            {
                double t = (42.5 + 5 * i);
                double m = Math.Round(pts.Points[i].Err_Distance * 1000, 1);
                if (i <= 3)
                {
                    sql = "Insert into [" + table_name + "] values('" + section.SectNo + "'," + t + "," + m +
                        ",'" + (cad.F_Points.Points[i].Coor.Coor_H - H).ToString() + "','" + (cad.F_Points.Points[i].Coor.Coor_V - V).ToString() + "')";
                    m_db.ExecuteSQL(sql);
                }
                else
                {
                    sql = "Insert into [" + table_name + "] values('" + section.SectNo + "'," + t + "," + m + ",'','')";
                    m_db.ExecuteSQL(sql);
                }

            }
        }
       //加载未完成的断面
       public void Load_unsolved()
        {
            try
            {

                if (unsolve.l_List.Count == 0 && unsolve.r_List.Count==0)
                {
                    string path = System.Windows.Forms.Application.StartupPath + @"\Logs\" + this.Text + ".txt";
                    if (File.Exists(path))
                    {
                        unsolve = Get_Clone(path) as unsolve_list;
                        if (unsolve.l_List.Count == 0 && unsolve.r_List.Count == 0)
                        {
                            return;
                        }
                    }
                }
                unsolve_tree.Nodes.Clear();
                for (int i = 0; i < unsolve.l_List.Count; i++)
                {
                    Node n = new Node();
                    n.Text = unsolve.l_List[i].name;
                    n.Cells.Add(new Cell(unsolve.l_List[i].reason));
                    unsolve_tree.Nodes.Add(n);
                }
                for (int i = 0; i < unsolve.r_List.Count; i++)
                {
                    Node n = new Node();
                    n.Text = unsolve.r_List[i].name;
                    n.Cells.Add(new Cell(unsolve.r_List[i].reason));
                    unsolve_tree.Nodes.Add(n);
                }
                unsolve_tree.Refresh();
            }
            catch
            {
                MessageBox.Show("Load_Unsolve Exception!");
            }
        }
        #endregion

        #region 关于树,zedgraph和sqldatabase的函数
        public void Load_tree()
        {
            try
            {
                advTree1.SelectedNode = null;
                advTree1.Nodes.Clear();
                Node n1 = new Node();
                Node n2 = new Node();
                System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(do_section_frm));
                n1.Image = ((System.Drawing.Image)(resources.GetObject("node1.Image")));
                n1.Text = "<font color=\"#388194\" size=\"18\">上行</font>";
                // 
                // node2
                // 
                n2.Image = ((System.Drawing.Image)(resources.GetObject("node2.Image")));
                n2.Text = "<font color=\"#388194\" size=\"18\">下行</font>";
                string sql;
                string table_name = "qj_" + m_prj.id.ToString() + "_" + m_prj.no.ToString();
                string name = "qj_" + m_prj.id.ToString();
                sql = "Select " + table_name + ".[断面号] from [" + table_name + "],[" + name + "] where " + name + ".[上下行]='L' and "
                    + table_name + ".[断面号]=" + name + ".[断面号] Group by "+name+".[断面号],"+table_name+".[断面号]";
                System.Data.DataTable dt;
                dt = m_db.GetDataSet(sql).Tables[0];
                List<int> names;
                names = order_data(dt);
                for (int i = 0; i < names.Count; i++)
                {
                    Node n = new Node();
                    n.Text = "<font color=\"#00B7EF\" size=\"10\">" + "L" + expand_id(names[i].ToString()) + "</font>";
                    n1.Nodes.Add(n);
                }
                sql = "Select " + table_name + ".[断面号] from [" + table_name + "],[" + name + "] where " + name + ".[上下行]='R' and "
                   + table_name + ".[断面号]=" + name + ".[断面号] Group by " + name + ".[断面号]," + table_name + ".[断面号]";
                dt = m_db.GetDataSet(sql).Tables[0];
                names = order_data(dt);
                for (int i = 0; i < names.Count; i++)
                {
                    Node n = new Node();
                    n.Text = "R" + expand_id(names[i].ToString());
                    n.Text = "<font color=\"#6F3198\" size=\"10\">" + "R" + expand_id(names[i].ToString()) + "</font>";
                    n2.Nodes.Add(n);
                }
                advTree1.Nodes.Add(n1);
                advTree1.Nodes.Add(n2);
                advTree1.Refresh();
            }
            catch
            {
                MessageBox.Show("LoadTree Exception");
                return;
            }
        }
        string expand_id(string name)
        {
            if (name.Length == 3)
            {
                return name;
            }
            else if (name.Length == 2)
            {
                return "0" + name;
            }
            else if (name.Length == 1)
            {
                return "00" + name;
            }
            else
            {
                return name.Substring(name.Length - 3, 3);
            }

        }
        List<int>  order_data(System.Data.DataTable dt)
        {
            List<int> str = new List<int>();
            try
            {
                
                
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string id = dt.Rows[i][0].ToString();
                    int int_id = Convert.ToInt32(id.Substring(1));
                    str.Add(int_id);
                }
                str.Sort();
            }
            catch( Exception e)
                {
                    message_frm frm = new message_frm(e.Message, "异常", 3);
                    frm.ShowDialog();
                    frm.Close();
                }
            return str;
        }
        #endregion


        #region 显示功能
        private void advTree1_AfterNodeSelect(object sender, AdvTreeNodeEventArgs e)
        {
            Node n = e.Node;
            if (n == null)
            {
                return;
            }
            zed.GraphPane.XAxis.Scale.Max = 3 * zed.GraphPane.Chart.Rect.Width / zed.GraphPane.Chart.Rect.Height;
            zed.GraphPane.XAxis.Scale.Min = -3 * zed.GraphPane.Chart.Rect.Width / zed.GraphPane.Chart.Rect.Height;
            zed.Refresh();

            if (n.Level == 0)
            {
                return;
            }
            data_memeber_section datamemeber = new data_memeber_section();
            if (n.Parent.Text.Contains( "上行")==true)
            {
                datamemeber.LR = "L";
            }
            else
            {
                datamemeber.LR = "R";
            }
            string table_name = "qj_" + m_prj.id.ToString() + "_" + no.ToString() + "_data";
            int id_index = e.Node.Text.IndexOf(datamemeber.LR);
            string id = e.Node.Text.Substring(id_index, 4);
            string sql = "Select * from [" + table_name + "] where [断面号]='" + id + "' order by [角度]";
            System.Data.DataTable dt = m_db.GetDataSet(sql).Tables[0];
            //初始化zed和zeddesign
            GraphPane myPane = zed.GraphPane;
            myPane.CurveList.Clear();
            myPane.Title.Text = id;
            GraphPane design_pane = zeddesign.GraphPane;
            design_pane.CurveList.Clear();
            design_pane.Title.Text = id;
            PointPairList list = new PointPairList();
            PointPairList designlist = new PointPairList();
            table_name = "qj_" + m_prj.id.ToString();
            sql = "select [H],[V] from [" + table_name + "] where [断面号]='" + id + "'";
            System.Data.DataTable c_dt = m_db.GetDataSet(sql).Tables[0];
            if (c_dt.Rows.Count == 0)
            {
                return;
            }
            double C_H = Convert.ToDouble(c_dt.Rows[0]["H"]);
            double C_V = Convert.ToDouble(c_dt.Rows[0]["V"]);
            c_dt.Dispose();

            //开始从数据库调数据
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                double angle = Convert.ToDouble(dt.Rows[i]["角度"]);
                double design_val = Convert.ToDouble(dt.Rows[i]["变形"]);
                designlist.Add(angle, design_val);
                angle = (angle - 90) * Math.PI / 180;
                double dist = 2.75 + design_val / 1000;
                double H = dist * Math.Cos(angle) ;
                double V = dist * Math.Sin(angle);
                list.Add(H, V);
            }
            LineItem myCurve2 = design_pane.AddCurve("设计变形", designlist, Color.Black,
                              SymbolType.Diamond);
            myCurve2.Line.IsVisible = false;
            myCurve2.Symbol.Size = 12;
            myCurve2.Symbol.Fill = new Fill(Color.Black);
            LineItem myCurve1 = myPane.AddCurve("拟合结果", list, Color.Cyan,
                              SymbolType.Diamond);
            myCurve1.Symbol.Fill = new Fill(Color.DarkBlue);

            for (int i = 0; i <= 3; i++)
            {
                PointPairList flist = new PointPairList();
                double H = Convert.ToDouble(dt.Rows[i][3]);
                double V = Convert.ToDouble(dt.Rows[i][4]);
                flist.Add(H, V);
                flist.Add(0, 0);
                LineItem F_line = myPane.AddCurve("", flist, Color.Red, SymbolType.None);
                F_line.Line.Width = 4;

            }


            //zedgrap收尾工作
           // zed.AxisChange();
            zed.Refresh();   
            zeddesign.AxisChange();
            zeddesign.Refresh();
            zedac.AxisChange();
            zedac.Refresh();
            zed.Refresh();

            //进行拟合信息的检索查询
            arc_info_tree.Nodes.Clear();
            table_name = "qj_" + m_prj.id.ToString() + "_" + no.ToString();
            sql = "select * from [" + table_name + "] where [断面号]='" + id + "'";
            dt = m_db.GetDataSet(sql).Tables[0];
            for (int i = 1; i <= 5; i++)
            {
                Node node = new Node();
                node.Text = "第" + i.ToString() + "环";
                string start="";
                string end="";
                string radius="";
                if (i == 1)
                {
                    start = "42.5";
                }
                else
                {
                    double s = Math.Round(SvAngle.RadToDms(Convert.ToDouble(dt.Rows[0]["A" + (i - 1).ToString()])), 1);
                    start = s.ToString();
                }
                if (i == 5)
                {
                    end = "317.5";
                }
                else
                {
                    double en = Math.Round(SvAngle.RadToDms(Convert.ToDouble(dt.Rows[0]["A" + i .ToString()])), 1);
                    end = en.ToString();

                }
                radius=Math.Round(Convert.ToDouble(dt.Rows[0]["R"+i.ToString()]),3).ToString();
                node.Cells.Add(new Cell(start));
                node.Cells.Add(new Cell(end));
                node.Cells.Add(new Cell(radius));
                arc_info_tree.Nodes.Add(node);
            }
            arc_info_tree.Refresh();


            //开始进行累计值计算
            GraphPane paneac = zedac.GraphPane;
    
            if (no == 1)
            {
                tabac.Visible = false;
                return;
            }
            paneac.CurveList.Clear();
            table_name = "qj_" + m_prj.id.ToString() + "_" + no.ToString() + "_data";
            string old_table = "qj_" + m_prj.id.ToString() + "_" + (no - 1).ToString() + "_data";//oldtable
            if (m_db.IsTableInDataBase(old_table) == false)
            {
                tabac.Visible = false;
                return;
            }
            tabac.Visible = true;
            sql = "Select [角度],[变形] from [" + old_table + "] where [断面号]='" + id + "' order by [角度]";
            System.Data.DataTable old_dt = m_db.GetDataSet(sql).Tables[0];
            if (old_dt == null || old_dt.Rows.Count == 0)
            {
                return;
            }
            PointPairList aclist = new PointPairList();
            for (int i = 0; i < 56; i++)
            {
                double angle = Convert.ToDouble(old_dt.Rows[i]["角度"]);
                double design_val = Convert.ToDouble(old_dt.Rows[i]["变形"]);
                double y = designlist[i].Y - design_val;
                aclist.Add(angle, y);
            }
            LineItem myCurve_ac = paneac.AddCurve("拟合结果", aclist, Color.DarkBlue,
                            SymbolType.Diamond);
            myCurve_ac.Line.IsVisible = false;
            myCurve_ac.Symbol.Size = 12;
            myCurve_ac.Symbol.Fill = new Fill(Color.Red);
            zedac.AxisChange();
            zedac.Refresh();




        }
        #endregion



        private void zed_SizeChanged(object sender, EventArgs e)
        {
            zed.GraphPane.XAxis.Scale.Max = 3 * zed.GraphPane.Chart.Rect.Width / zed.GraphPane.Chart.Rect.Height;
            zed.GraphPane.XAxis.Scale.Min = -3 * zed.GraphPane.Chart.Rect.Width / zed.GraphPane.Chart.Rect.Height;
            zed.Refresh();
        }

        private void do_section_frm_SizeChanged(object sender, EventArgs e)
        {
            try
            {
                zed.GraphPane.XAxis.Scale.Max = 3 * zed.GraphPane.Chart.Rect.Width / zed.GraphPane.Chart.Rect.Height;
                zed.GraphPane.XAxis.Scale.Min = -3 * zed.GraphPane.Chart.Rect.Width / zed.GraphPane.Chart.Rect.Height;
                zed.Refresh();
            }
            catch
            {
                return;
            }
        }

        private void do_section_frm_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
               
                    zed.GraphPane.CurveList.Clear();
                    zed.Dispose();
                
                string path = System.Windows.Forms.Application.StartupPath + @"\Logs\" + this.Text + ".txt";
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                CloneObject(unsolve, path);
            }
            catch
            {
 
            }
        }

        private void unsolve_tree_NodeDoubleClick(object sender, TreeNodeMouseEventArgs e)
        {
            Node n = e.Node;
            int index = n.Index;
            add_in_frm frm;
            if (index < unsolve.l_List.Count)
            {
                this.LR = "L";
                frm = new add_in_frm(this, unsolve.l_List[index]);
                
            }
            else
            {  
                LR = "R";
                frm = new add_in_frm(this, unsolve.r_List[index - unsolve.l_List.Count ]);
              
            }
           
            frm.ShowDialog();
        }

        private void btn_handle_Click(object sender, EventArgs e)
        {
            Node n = unsolve_tree.SelectedNode;
            if (n == null)
            {
                message_frm frm1 = new message_frm("先选中了数据再处理!\r\n好伐?", "错误", 0);
                frm1.ShowDialog();
                frm1.Close();
                return;
            }
            int index = n.Index;
            add_in_frm frm;
            if (index <= unsolve.l_List.Count)
            {
                frm = new add_in_frm(this, unsolve.l_List[index]);
            }
            else
            {
                frm = new add_in_frm(this, unsolve.r_List[index - unsolve.l_List.Count-1]);
            }
            frm.ShowDialog();
        }

        private void btn_ignore_Click(object sender, EventArgs e)
        {
            Node n = unsolve_tree.SelectedNode;
            if (n == null)
            {
                message_frm frm1 = new message_frm("先选中了数据再操作!\r\n好伐?", "错误", 3);
                frm1.ShowDialog();
                frm1.Close();
                return;
            }
            int index = n.Index;
            if (index < unsolve.l_List.Count)
            {
                unsolve.l_List.RemoveAt(index);
            }
            else
            {
                unsolve.r_List.RemoveAt(index - unsolve.l_List.Count );
            }
            Load_unsolved();
        }

        private void btn_add_in_Click(object sender, EventArgs e)
        {
            add_in_frm frm = new add_in_frm(this);
            frm.ShowDialog(this);
        }

        private void btn_ignoreall_Click(object sender, EventArgs e)
        {
            unsolve.r_List.Clear();
            unsolve.l_List.Clear();
            unsolve_tree.Nodes.Clear();
        }

        private void ribbonClientPanel1_Click(object sender, EventArgs e)
        {

        }

        #region 序列化读取未完成的断面
        public object Get_Clone(string path)
        {
            using (FileStream sw = new FileStream(path, FileMode.Open))
            {
                BinaryFormatter bf = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone));
                object CloneObject;
                CloneObject = bf.Deserialize(sw);
                sw.Close();
                return CloneObject;
            }
        }
        public void CloneObject(object obj, string path)
        {
            //若重复计算，则每次清空文件
            if (File.Exists(path) == true)
            {
                File.Delete(path);
            }
            MemoryStream m = new MemoryStream();

            using (FileStream sw = new FileStream(path, FileMode.Create))
            {

                BinaryFormatter bf = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone));
                //bf.Serialize(sw, obj);
                //bf.Serialize(m, obj);
                //m.Write(
                sw.Seek(0, SeekOrigin.Begin);
                sw.Close();
                
            }

        }
        #endregion



    }
}
